package com.itheima.health.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Menu;
import com.itheima.health.service.MenuService;
import com.itheima.health.service.UserService;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * @author ：gaoqiang
 * @date ：Created in 2019/7/3
 * @description ：
 * @version: 1.0
 */
@RestController
@RequestMapping("/menu")
public class MenuController {
    @Reference
    private MenuService menuService;

    @Reference
    private UserService userService;

    @RequestMapping("/findMenuByRoleId")
    public Result findMenuByRoleId() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        User user = (User) authentication.getPrincipal();
        String username = user.getUsername();
        com.itheima.health.pojo.User user1 = userService.findByUsername(username);
        Integer id1 = user1.getId();
        Integer id = userService.findRoleIdByUserId(id1);
        try {
            List<Map<String, Object>> menuList = menuService.findMenuByRoleId(id);
            return new Result(true, "成功", menuList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "失败");
        }
    }

    @RequestMapping("/add")
    public Result add(@RequestBody Menu menu) {
        try {
            menuService.add(menu);
            return new Result(true, "添加菜单成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "添加菜单失败");
        }

    }

    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        PageResult pageResult = null;
        try {

            pageResult = menuService.pageQuery(queryPageBean.getCurrentPage(),
                    queryPageBean.getPageSize(), queryPageBean.getQueryString());
            return pageResult;
        } catch (Exception e) {
            e.printStackTrace();
            return new PageResult(0L, null);
        }

    }
    @RequestMapping("/findById")
    public Result findById(Integer id){
        try {
           Menu menu= menuService.findById(id);
           return new Result(true,"回显成功",menu);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"回显失败");
        }

    }
    @RequestMapping("/edit")
    public Result edit(@RequestBody Menu menu){
        try {
            menuService.edit(menu);
            return new Result(true,"编辑成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"编辑失败");
        }

    }
    @RequestMapping("/deleteById")
    public Result deleteById(Integer id){
        try {
            menuService.deleteById(id);
            return new Result(true,"删除菜单成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,e.getMessage());
        }
    }
}
